package com.anttribe.components.db.metadata.entity;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * JDBC类型
 *
 * @author zhaoyong
 * @version 2015年12月21日
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum JdbcType {

    ARRAY(java.sql.Types.ARRAY),
    BIGINT(java.sql.Types.BIGINT, 20, 0),
    BINARY(java.sql.Types.BINARY),
    BIT(java.sql.Types.BIT, 1, 0),
    BLOB(java.sql.Types.BLOB),
    BOOLEAN(java.sql.Types.BOOLEAN),
    CHAR(java.sql.Types.CHAR, 255),
    CLOB(java.sql.Types.CLOB),
    DATALINK(java.sql.Types.DATALINK),
    DATE(java.sql.Types.DATE),
    DECIMAL(java.sql.Types.DECIMAL, 10, 0),
    DISTINCT(java.sql.Types.DISTINCT),
    DOUBLE(java.sql.Types.DOUBLE, 10, 0),
    FLOAT(java.sql.Types.FLOAT, 10, 0),
    INTEGER(java.sql.Types.INTEGER),
    JAVA_OBJECT(java.sql.Types.JAVA_OBJECT),
    LONGNVARCHAR(java.sql.Types.LONGNVARCHAR),
    LONGVARBINARY(java.sql.Types.LONGVARBINARY),
    LONGVARCHAR(java.sql.Types.LONGVARCHAR),
    NCHAR(java.sql.Types.NCHAR),
    NCLOB(java.sql.Types.NCLOB),
    NULL(java.sql.Types.NULL),
    NUMERIC(java.sql.Types.NUMERIC, 10, 2),
    NVARCHAR(java.sql.Types.NVARCHAR),
    OTHER(java.sql.Types.OTHER),
    REAL(java.sql.Types.REAL),
    REF(java.sql.Types.REF),
    ROWID(java.sql.Types.ROWID),
    SMALLINT(java.sql.Types.SMALLINT),
    SQLXML(java.sql.Types.SQLXML),
    STRUCT(java.sql.Types.STRUCT),
    TIME(java.sql.Types.TIME),
    TIMESTAMP(java.sql.Types.TIMESTAMP),
    TINYINT(java.sql.Types.TINYINT),
    VARBINARY(java.sql.Types.VARBINARY),
    VARCHAR(java.sql.Types.VARCHAR, 255),
    VARCHAR2(java.sql.Types.VARCHAR, 255);

    private int sqlType;

    /**
     * 默认长度
     */
    private int size;

    /**
     * 默认精度位
     */
    private int precision;

    /**
     * 默认小数位
     */
    private int digits;

    JdbcType(int sqlType) {
        this.sqlType = sqlType;
    }

    JdbcType(int sqlType, int size) {
        this.sqlType = sqlType;
        this.size = size;
    }

    JdbcType(int sqlType, int precision, int digits) {
        this.sqlType = sqlType;
        this.precision = precision;
        this.digits = digits;
    }

    public static JdbcType valueOf(int sqlType) {
        JdbcType[] types = JdbcType.values();
        for (JdbcType jdbcType : types) {
            if (jdbcType.sqlType == sqlType) {
                return jdbcType;
            }
        }
        return null;
    }

}
